from flask import Blueprint, jsonify, request
from utils.db import db
from utils.comm import get_data
from utils.redis import r
import datetime

bp_work = Blueprint('bp_work', __name__)


@bp_work.route('/yd_login', methods=['POST'])
def yd_login():
    data = get_data()
    sql = "select * from user where username='%s' and password='%s'" % (data['username'], data['password'])
    res = db.find(sql)
    print(res, 22222222222222222222222)
    list1 = []
    if res:
        role_id = res['role_id']
        # 根据角色id查询资源列表
        sql1 = "select b.name,b.url from role_ziyuan as a left join ziyuan as b on " \
               "a.ziyuan_id=b.id where a.role_id=%d" % (int(role_id))
        list1 = db.find_all(sql1)
    print(list1, 11111111111111111111111111)
    return jsonify({'code': 200, 'list1': list1, 'id': res['id'], 'username': res['username']})


@bp_work.route('/post_news', methods=['POST'])
def post_news():
    data = get_data()
    print(data, 2222222222222222222222222222222222222222222222222222222222222)
    sql = "insert into news(title,status,user_id) values ('%s',%d,%d)" % (data['title'], 1,1)
    db.update(sql)
    return jsonify({'code': 200})


@bp_work.route('/get_news')
def get_news():
    sql = "select * from news where status=%d" % (1)
    res = db.find_all(sql)
    return jsonify({'code': 200, 'list': res})


@bp_work.route('/tongguo', methods=['GET', 'POST'])
def tongguo():
    data = get_data()
    now = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d')
    sql = "select user_id from news where id=%d" % (int(data['id']))
    res = db.find(sql)
    # 添加到记录表
    sql1 = "insert into score_goods(user_id,goods_id,score,ctime) values (%d,%d,%d,'%s')" % (
        int(res['user_id']), int(data['id']), 5, str(now))
    db.update(sql1)
    # 修改状态
    sql2 = "update news set status=%d where id=%d" % (2, int(data['id']))
    db.update(sql2)
    # 添加积分
    sql3 = "select * from score_record where user_id=%d" % (int(res['user_id']))
    res1 = db.find(sql3)
    if res1:
        score = int(res1['score']) + 5
        sql4 = "update score_record set score=%d where user_id=%d" % (score, int(res['user_id']))
        db.update(sql4)
        return jsonify({'code': 200, 'msg': '添加成功'})
    else:
        sql5 = "insert into score_record(user_id,score) values (%d,%d)" % (int(res['user_id']), 5)
        db.update(sql5)
        return jsonify({'code': 200, })


@bp_work.route('/notongguo', methods=['POST'])
def notongguo():
    data = get_data()
    sql = "update news set status=%d where id=%d" % (3, int(data['id']))
    db.find_all(sql)
    return jsonify({'code': 200})


# 添加商品
@bp_work.route('/post_goods', methods=['POST', 'GET'])
def post_goods():
    data = get_data()
    if request.method == 'POST':
        sql = "insert into goods(name,score) values ('%s',%d)" % (data['name'], int(data['score']))
        db.update(sql)
        return jsonify({'code': 200})
    else:
        sql = "select * from goods "
        res = db.find_all(sql)
        return jsonify({'code': 200, 'list': res})


# 柱状图
@bp_work.route('/get_echart')
def get_echart():
    data = get_data()
    sql = "select sum(score) as s,ctime from score_goods where user_id=1 group by ctime"
    res = db.find_all(sql)
    list = []
    list1 = [str(i['ctime']) for i in res]
    list2 = [float(i['s']) for i in res]
    list.append(list1)
    list.append(list2)
    return jsonify({"code": 200, "list": list})
